package com.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.entity.Cake;
import com.entity.ConditionCakeType;
import com.google.gson.Gson;
import com.servers.CakeServers;

@Controller
public class CakeController {
	@Autowired
	private CakeServers cakeServers;
	
	@RequestMapping("/load")
	public String load() {
		System.out.println("------------进入到load----------------");
		
		return "success";
	}
	@RequestMapping("/commodity")
	public String commodity() {
		System.out.println("------------进入到commodity----------------");
		
		return "commodity";
	}
	@ResponseBody
	@RequestMapping("/getcommodity")
	public String getcommodity() {
		List<Cake> clist= cakeServers.getAllCakes();
		/*for (Cake cake : clist) {
			System.out.println(cake);
		}*/
		
		//   {"img":"../res/img/paging_img3.jpg","title":"森系小清新四件套","pri":"￥200","nub":"1266付款"}
		//封装json
		Map<String, Object> map1=new HashMap<>();
		map1.put("status", 0);
		//封装menu的 milk value值
		Map<String, Object> map2=new HashMap<>();
		//封装content的value值
		Map<String, Object> map3=new HashMap<>();
		// 分装 content下的 数组,图片的目录
		List<Map<String, Object>> list=new ArrayList<>();
		//封住img的路径
		for (Cake cake : clist) {
			Map<String, Object> map4=new HashMap<>();
			map4.put("img", "images/Fruits/"+cake.getCakeName()+".png");
			map4.put("title", cake.getCakeName());
			map4.put("pri", cake.getCakePrice());
			map4.put("nub", "888付款");
			list.add(map4);
		}
		map2.put("content", list);
		//封装menu的 milk value值
		Map<String, Object> mapMilk=new HashMap<>();
		mapMilk.put("milk",map2 );
		map1.put("menu", mapMilk);
		map1.put("hid", 8);
		Gson gson=new Gson();
		//System.out.println("gson:"+gson.toJson(map1));
		return gson.toJson(map1);
	}
	@RequestMapping("/details11")
	public String details111(String name,Model model) {
		System.out.println("-----------------进入到details 详情 页面--------------------");
		System.out.println("name:"+name);
		Cake cake = cakeServers.getCakeByName(name);  
		model.addAttribute("imgurl",cake.getCakeName() );
		model.addAttribute("context",cake.getCakeContext() );
		model.addAttribute("price",cake.getCakePrice() );
		return "details";
		//return "details";
	}
	@RequestMapping("/details")
	public ModelAndView getDetail111(String name){
		ModelAndView av=new ModelAndView();
		av.setViewName("detailsInfo");
		System.out.println("name:"+name);
		Cake cake= cakeServers.getCakeByName(name);
		System.out.println("---------"+cake);
		av.addObject("imageInfo", cake.getCakeName());
		av.addObject("price", cake.getCakePrice());
		av.addObject("content", cake.getCakeContext());
		return av;
	}
	
	@ResponseBody
	@RequestMapping("/getIndexJSON")
	public String getIndexJSON() {
		Map<String, Object> map =new HashMap<>();
		Map<String, Object> indexJson = cakeServers.getIndexJson("floor");
		Map<String, Object> indexJson2 = cakeServers.getIndexJson("hotCon");
		Map<String, Object> indexJson3 = cakeServers.getIndexJson("proct");
		Map<String, Object> indexJson4 = cakeServers.getIndexJson("productList");
		indexJson.putAll(indexJson2);
		indexJson.putAll(indexJson3);
		indexJson.putAll(indexJson4);
		indexJson.put("status",0);
/*		map.put("status",0);
		map.put("floor",indexJson);
		map.put("hotCon",indexJson2);
		map.put("proct",indexJson3);
		map.put("productList",indexJson4);*/
		Gson gson=new Gson();
		//System.out.println(gson.toJson(map));
		return gson.toJson(indexJson);
	}
	@ResponseBody
	@RequestMapping("/getTypeCake")
	public String getTypeCake(String cakeTypeId,String hiddemo) {
		int intTypeId=Integer.parseInt(cakeTypeId);
		ConditionCakeType conditionCakeType=new ConditionCakeType(intTypeId, null, null);
		Map<String, Object> commodityMilkJson = cakeServers.getCommodityMilkJson(conditionCakeType);
		Map<String, Object> map =new HashMap<>();
		map.put("status", 0);
		map.put("menu", commodityMilkJson);
		map.put("hid", 8);
		Gson gson=new Gson();
		//  1销量 2 价格 3 新品 4 收藏 
		//收藏 后续 再添加
		System.out.println("hiddenmo:"+hiddemo);
		
		return gson.toJson(map);
	}
	@RequestMapping("/gotusi")
	public String gotusi() {
		return "tusi";
	}
	
	@ResponseBody
	@RequestMapping("/getTUSIjson")
	public String gobuytoday() {
		ConditionCakeType conditionCakeType=new ConditionCakeType(6, null, null);
		ConditionCakeType conditionCakeType2=new ConditionCakeType(7, null, null);
		ConditionCakeType conditionCakeType3=new ConditionCakeType(9, "cakesales", "desc");
		Map<String, Object> getgoBuytodayJson = cakeServers.getgoBuytodayJson(conditionCakeType, "productList","tusi");
		Map<String, Object> getgoBuytodayJson2 = cakeServers.getgoBuytodayJson(conditionCakeType2, "productList","lipin");
		Map<String, Object> getgoBuytodayJson3 = cakeServers.getgoBuytodayJson(conditionCakeType3, "footerList",null);
		
		Map<String, Object> map =new HashMap<>();
		
		map.put("status", 0);
		map.putAll(getgoBuytodayJson);
		map.putAll(getgoBuytodayJson2);
		map.putAll(getgoBuytodayJson3);
		Gson gson=new Gson();
		
		return gson.toJson(map); 
	}
	
	
	public void setCakeServers(CakeServers cakeServers) {
		this.cakeServers = cakeServers;
	}

}
